<?php
/*
* Class teia\webdev\php\core\UserProfile
* Core class for User profile
*
* @author Bernardo Quibiana
* @date created: 20111028
*
* @date modified: 20111028
* @modified: Bernardo Quibiana
*/

// namespace declaration: Abstract Data Types
namespace teia\webdev\php\core;

class UserProfile {
	protected $usr_id;
	protected $usr_grp;
	protected $prof_id;
	protected $given_name;
	protected $family_name;
	protected $birthday;
	protected $gender;
	protected $street_addr1;
	protected $street_addr2;
	protected $postal_code;
	protected $city;
	protected $state;
	protected $country_code;
	protected $home_phone;
	protected $work_phone;
	protected $mobile_phone;
	protected $prefer_phone;
	
	public function __construct($prof_id, $usr_id, $usr_grp){
		$this->prof_id = $prof_id;
		$this->usr_id = $usr_id;
		$this->usr_grp = $usr_grp;
		
		$this->db_fetch($prof_id, $usr_id, $usr_grp);
	}
	
	/**
	* Enter description here ...
	* @return multitype:string
	*/
	public function __sleep(){
		return array('prof_id', 'usr_id', 'usr_grp');
	}
	
	/**
	 * 
	 */
	public function __wakeup(){
		$this->db_fetch($this->prof_id, $this->usr_id, $this->usr_grp);
	}
	
	protected function db_fetch($prof_id, $usr_id, $usr_grp) {
		$query = "SELECT * FROM sec_usr_prof WHERE prof_id=$this->prof_id AND usr_id='$this->usr_id' AND usr_grp='$this->usr_grp' COUNT 1";
		
		try {
			$db_link = MySqlLink::link_factory();
			$result = $db_link->query($query);
			if ($result) {
				$result_row = mysql_fetch_assoc($result);
				foreach ($result_row as $key => $value) {
					@$this->$key = $value;	// suppress errors on undefined properties
				}
			} else {
				throw new \Exception("Error: Could not fetch profile \"$prof_id\" for user \"$usr_id\" in group \"$usr_grp\"", 100);
			}
		} catch (\Exception $e) {
			throw $e;
		}
	}
	
	public static function create_basic_profile($usr_id, $usr_grp, $birthday, $gender) {
		$query = "INSERT INTO sec_usr_prof(usr_id,usr_grp,birthday,gender) values('$usr_id','$usr_grp','$birthday','$gender')";
		
		try {
			$db_link = MySqlLink::link_factory();
			$result = $db_link->verication_query($query);
			
			return $result;
		} catch (\Exception $e) {
			throw $e;
		}
	}
}
?>